﻿using Microsoft.Phone.Controls;
using System;

namespace TheVerge.Phone.Controls
{
    public class ItemDrawnArgs : EventArgs
    {
        public object DataItem { get; set; }
    }
    public delegate void ItemDrawnHandler(object sender, ItemDrawnArgs args);

    public class InfiniteListbox : LongListSelector
    {
        public event EventHandler AtTop = delegate { };
        public event ItemDrawnHandler ItemDrawn = delegate { };

        public InfiniteListbox()
        {
            

#if WP_8
            IsGroupingEnabled = false;
            ItemRealized += InfiniteListbox_ItemRealized;
            ManipulationStateChanged += InfiniteListbox_ManipulationStateChanged;
#else
            //-- WP7
            //Loaded += InfiniteListbox_Loaded;
            Link += InfiniteListbox_Link;
            StretchingTop += InfiniteListbox_StretchingTop;
            IsFlatList = true;
#endif
        }

        

#if WP_8

        private void InfiniteListbox_ManipulationStateChanged(object sender, EventArgs e)
        {
            if (ManipulationState == System.Windows.Controls.Primitives.ManipulationState.Animating)
            {
                var vp = FindViewport(this);
                if (vp.Viewport.Top == 0)
                    AtTop(this, EventArgs.Empty);
            }
        }

        private void InfiniteListbox_ItemRealized(object sender, ItemRealizationEventArgs e)
        {
            if (e.ItemKind == Microsoft.Phone.Controls.LongListSelectorItemKind.Item)
            {
                var dt = e.Container.DataContext;
                var args = new ItemDrawnArgs();
                args.DataItem = dt;
                ItemDrawn(this, args);
            }
        }

        private static System.Windows.Controls.Primitives.ViewportControl FindViewport(System.Windows.DependencyObject parent)
        {
            var childCount = System.Windows.Media.VisualTreeHelper.GetChildrenCount(parent);
            for (var i = 0; i < childCount; i++)
            {
                var elt = System.Windows.Media.VisualTreeHelper.GetChild(parent, i);
                if (elt is System.Windows.Controls.Primitives.ViewportControl) 
                    return (System.Windows.Controls.Primitives.ViewportControl)elt;
                var result = FindViewport(elt);
                if (result != null) return result;
            }
            return null;
        }
#else
        private void InfiniteListbox_StretchingTop(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                AtTop(this, EventArgs.Empty);
            });
        }

        private void InfiniteListbox_Link(object sender, LinkUnlinkEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                var args = new ItemDrawnArgs();
                args.DataItem = e.ContentPresenter.Content;
                ItemDrawn(this, args);
            });
        }
#endif
    }
}
